/**
 * 
 */
package com.app.mvc.action.file;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.app.dao.po.CornDbDna;
import com.app.dao.po.CornFiles;
import com.app.service.ICornDBService;
import com.app.service.IFileService;
import com.app.service.vo.CornDbDnaVo;

/**
 * @author Administrator
 * 文件处理
 */
@RequestMapping(value="/file")
@Controller
public class FIleAction {

	/**
	 * 
	 */
	public FIleAction() {
		logger.info(this.getClass().getName()+" loader");
	}
	
	private static Logger logger = Logger.getLogger(FIleAction.class);

	@Autowired
	private IFileService fileService;
	@Autowired
	private ICornDBService cornDBService;
	/**
	 * 
	 * @param json
	 * @return
	 */

	@RequestMapping(value="/upload.form")
	@ResponseBody
	public Object upload(@RequestParam("file") MultipartFile file,String cornid,HttpServletRequest request)
	{
		logger.info("种子信息添加");
		String path=request.getRealPath("/uploadfiles");
		String fileType=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String filename="/"+System.currentTimeMillis()+fileType;
		String uploadPath=path+filename;
		String filePath="/uploadfiles"+filename;
		try {
			FileCopyUtils.copy(file.getBytes(),new File(uploadPath));
		} catch (IOException e) {
			e.printStackTrace();
		} 
		CornFiles cornFile=new CornFiles();
		CornDbDnaVo dnaVo=cornDBService.getCornDbDnaVoById(cornid.trim());
		CornDbDna dna=new CornDbDna();
		try {
			PropertyUtils.copyProperties(dna,dnaVo);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		cornFile.setCornDbDna(dna);
		cornFile.setFilePath(filePath);
		cornFile.setFileName(filename);
		fileService.save(cornFile);
		return "message:'success'";
	}
	
	@RequestMapping(value="/fileList.form")
    public String filelist(@RequestParam(value="id") String id,ModelMap model){
		//model
		model.put("list", fileService.selectFileByCornDBId(id));
		return "/file/file-list";
	}
	
	
}
